

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
/**
 * responsible for get the data from the server - the last
 * changes in the server
 * 
 * @author @author Vladimir Sarkisyan & Erez 
 */

public class ChangeChekerProtocol {
	 BufferedReader in;
	 PrintWriter out;
	 String response=null;
	 String lastChange;
	 CheckingDataSocket chDataSoc;
	 
	 public ChangeChekerProtocol(BufferedReader in, PrintWriter out){
	  this.in=in;this.out=out;
	  lastChange = null;
	  
	  //initialize
	  chDataSoc = new CheckingDataSocket();
	 }
	 
	 /**
	  * checking if there was changes in the server
	  * 
	  * get the string of last change from the server, if it is 
	  * not equal to the lastChange, so create CheckingDataSocket
	  * that will be check with the search program, if it is updated
	  * in the DB, if not, it go to inner loop that will be 
	  * checking with search program the changes until get to one
	  * that all ready updated- means that don't need to be change
	  * 
	  */
	 public void run(){
	  BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
	  String request=null;
	  String updated = null;
	  int numberOfTheChange;
	  String []arr;
	  
	  try {
	   while(true){
	    System.out.println(">");
	    try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    //update the number
	    numberOfTheChange = 0;
	    //number of the change - from the last
	    
	    out.println(numberOfTheChange);
	    
	    //the change from the server
	    request = in.readLine();
	    
	    System.out.println("ChangeChekerProtocol request - "+ request);
	    
	    if(request==null){
	    	System.out.println("in if request==null");
	    }	    
	    if(request!=null){
	    	System.out.println("request " + request);
	    }
	    
	    //check if the request is different
	    if(!(request.equals("null"))&&(request!=null)&&(!request.equals(lastChange))){
	    	System.out.println("in if");
	    	//split
			arr = request.split(" ");	    	
	    	
	 	    //send to check the request
			updated = chDataSoc.run(request);
			//updated = new CheckingDataSocket().run(request);  
	    	
	    	System.out.println("ChangeChekerProtocol updated - "+ updated);
	    	//update the last change

	    	lastChange = request;

	    	
	    	//update the all changes
	    	while((updated.equals("false"))&&(!(request.equals("null")))){
	    		
	    	    //number of the change - from the last
	    		System.out.println("numberOfTheChange:" + numberOfTheChange);
	    	    out.println(numberOfTheChange);
	    	    //the change from the server
	    	    request = in.readLine();
	    	    System.out.println("ChangeChekerProtocol while request - "+ request);
		    	//split
	    	    
	    	    //if it is not a null
	    	    if(!request.equals("null")){
					arr = request.split(" ");
					
					//send to check the request
		    		updated = chDataSoc.run(request);
		    		System.out.println("ChangeChekerProtocol  updated: " + updated);
		    		
		    		//if it is a new file
		    		if(arr[0].equals("created")){
		    			//send a sign that file need be transfered
		    			out.println("createFile");
		    		}
	
		    		numberOfTheChange++;	    	    	
	    	    }
	    	    //end of if check if it is a null
	    	}
	    	//end while
	    }
	    
	    /*
	    request=sysin.readLine();
	    out.println(request);
	    response=in.readLine();
	    System.out.println(response);
	    */
	   }
	   //end of the prime while
	  } 
	  catch (IOException e) {
	  }
	}
	 //end run
	 
	 public void stopRun(){
		 response = "bye";
	 }
	 
}
